#-*- coding:utf-8 -*-

import re
import datetime
import copy
import sys
import threading

def get_dist(a,b):
        dist = 0
	
	###过滤快乐星期天
	if len(a) > 7:
		a.pop()
	if len(b) > 7:
		b.pop()
	###过滤快乐星期天
	
	if a.pop() == b.pop():
		dist+=1
        for i in a:
                for  j in b:
                        if i==j:
                                dist+=1
        return dist


#my_dist = int(sys.argv[1])

def get_lott_his():
    lott_his = []
    with open('lottery_his.txt','r') as f:
        lines = f.readlines()[1:]
                #print(len(lines))
        for line in lines:
            ball_list = []
            balls = re.split(',|\\\\',line.strip().split()[-1])
            #reds = balls.split(',\\')
            #for ball in reds:
            #   ball_list.append(ball)
            #blue = balls.split('\\')
            #lott_his.append(balls)#生成字符串列表
            lott_his.append(map(int,balls))
    return(lott_his)
#	with open('lott_list.csv','w+') as wf:
#		for his in lott_his:
#			his_line = ''
#			for ball in his:
#				his_line += ball
#				his_line += ','
#			his_line = his_line.strip(',')
#			his_line +='\n'
#			wf.write(his_line)
def calc_his():
	start = datetime.datetime.now()
	a = 0
	dist_count = 0
	lenth = len(lott_his)	
	for i in range(lenth):
		for j in range(i+1,lenth):
			a+=1
			#if lott_his[i] ==lott_his[j]:
			#	print(lott_his[i])
			if a%100000 == 0:
				print(a)
			lott1 = copy.copy(lott_his[i])
			lott2 = copy.copy(lott_his[j])
			dist = get_dist(lott1,lott2)
			if dist <= my_dist:
				dist_count+=1
			#	print(lott_his[i]),
			#	print('||'),
			#	print(lott_his[j]),
			#	print('==>'),
			#	print(dist)
			#	print('\n')
		#	del lott1,lott2
	print(a)
	stop = datetime.datetime.now()
	last = (stop-start).seconds
	print('持续运行：'),
	print(last)

	print('匹配数量: '),
	print(dist_count)
	
	print('概率：'),
	print(float(dist_count)/float(a))

def filter():

    pool = []



    lott_his = get_lott_his()

    denominator = float(len(lott_his))
    print('denominator:'),
    print(denominator)
    print('type:')
    print(type(denominator))

    for a in range(1,29):
        t = threading.Thread(target = single_filter, args = (a,lott_his,denominator))
        t.start()
        pool.append(t)

    for t in pool:
        t.join()


    # file_handle = open('filter.txt','w+')

    # for a in range(1,29):
    # 	for b in range(a+1,30):
    # 		for c in range(b+1,31):
    # 			for d in range(c+1,32):
    # 				for e in range(d+1,33):
    # 					for f in range(e+1,34):
    # 						for g in range(1,17):
    # 							candicate = [a,b,c,d,e,f,g]
    # 							dist = 0
    # 							continue_flag = 0
    # 							for his in lott_his:
    # 								cp1 = copy.copy(candicate)
    # 								cp2 = copy.copy(his)
    # 								this_dist = get_dist(cp1,cp2)
    # 								if this_dist ！= 0:
    # 									continue_flag = 1
    # 									break
    # 								dist += this_dist
    # 							if continue_flag ==1:
    # 								continue
    # 							avg_dist = float(dist)/denominator

    # 							line=str(a)+','+str(b)+','+str(c)+','
    # 								+str(d)+','+str(e)+','+str(f)+','+str(g)+' '+
    # 								'	'+str(avg_dist)+'\n'
    # 							file_handle.write(line)



def single_filter(start,lott_his,denominator):
    file = './ffile/filter_' + str(start) + '.txt'
    file_handle = open(file,'w+')
    file_handle.write('start from:' + datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S') + '\n')
    for b in range(start+1,30):
        for c in range(b+1,31):
            for d in range(c+1,32):
                for e in range(d+1,33):
                    for f in range(e+1,34):
                        for g in range(1,17):
                            candicate = [start,b,c,d,e,f,g]
                            dist = 0
                            continue_flag = 0
                            for his in lott_his:
                                cp1 = copy.copy(candicate)
                                cp2 = copy.copy(his)
                                this_dist = get_dist(cp1,cp2)
                                if this_dist > 4:
                                    continue_flag = 1
                                    break
                                dist += this_dist
                            if continue_flag ==1:
                                continue
                            avg_dist = float(dist)/denominator

                            line=str(start)+','+str(b)+','+str(c)+','+str(d)+','+str(e)+','+str(f)+','+str(g)+' '+'   '+str(avg_dist)+'\n'

                            file_handle.write(line)


    file_handle.write('stop at:' + datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S') + '\n')
    file_handle.close()                                 

start = datetime.datetime.now()
print('start:'+start.strftime('%Y-%m-%d %H-%M-%S-%f'))
filter()
end = datetime.datetime.now()
print('stop:'+end.strftime('%Y-%m-%d %H-%M-%S-%f'))

print((end - start).seconds)
